package vn.personal.anhht.bloback12.database;




import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class CallBlockDatabaseAdapter {
	
	
	public static final String KEY_ID = "id";
	public static final String KEY_NAME = "fullname";
	public static final String KEY_PHONE = "phone";
	public static final String KEY_GROUP = "group";
	public static final String KEY_TYPE = "type";
	public static final String KEY_IS_BLOCK_CALL = "isBlockCall";
	public static final String KEY_IS_BLOCK_SMS = "isBlockSms";
	public static final String KEY_IS_WHITE_LIST = "isWhiteList";
	
	public static final String TABLE_BLACKLIST = "blacklists";
	
	private Context context;
	private SQLiteDatabase database;
	private CallBlockDatabaseHelper dbHelper;
	
	
	public CallBlockDatabaseAdapter(){
		this.context = null;
		dbHelper = new CallBlockDatabaseHelper(context);
	}
	
	public CallBlockDatabaseAdapter(Context _context){
		this.context = _context;
		dbHelper = new CallBlockDatabaseHelper(context);
	}
	
	public CallBlockDatabaseAdapter open() throws SQLException {
		database = dbHelper.getWritableDatabase();
		return this;
	}
	
	public void close(){
		dbHelper.close();
	}

	public android.database.Cursor selectAllBlackList(){
		String[] columns = {KEY_ID, KEY_NAME, KEY_PHONE, KEY_IS_BLOCK_CALL, KEY_IS_BLOCK_SMS, KEY_IS_WHITE_LIST};
		String selection = null;
		String[] selectionAgrs = null;
		String groupBy = null;
		String having = null;
		String orderBy = KEY_NAME;
		return this.database.query(TABLE_BLACKLIST, columns, selection, selectionAgrs, groupBy, having, orderBy);
	}
	
	public Cursor selectBlackListByKeyWord(String keyWord){
		String[] columns = {KEY_ID, KEY_NAME, KEY_PHONE, KEY_IS_BLOCK_CALL, KEY_IS_BLOCK_SMS, KEY_IS_WHITE_LIST};
		String selection = KEY_NAME + " LIKE '%" + keyWord + "%' OR " + KEY_PHONE + " LIKE '%" + keyWord + "%'";;
		String[] selectionAgrs = null;
		String groupBy = null;
		String having = null;
		String orderBy = KEY_NAME;
		return this.database.query(TABLE_BLACKLIST, columns, selection, selectionAgrs, groupBy, having, orderBy);
	}
	
	public android.database.Cursor selectBlackListById(int _id){
		String[] columns = {KEY_ID, KEY_NAME, KEY_PHONE, KEY_IS_BLOCK_CALL, KEY_IS_BLOCK_SMS, KEY_IS_WHITE_LIST};
		String selection = KEY_ID + " = " + _id;
		String[] selectionAgrs = null;
		String groupBy = null;
		String having = null;
		String orderBy = KEY_NAME;
		return this.database.query(TABLE_BLACKLIST, columns, selection, selectionAgrs, groupBy, having, orderBy);
	}
	
	public Cursor selectBlackListByPhone(String phone){
		String[] columns = {KEY_ID, KEY_NAME, KEY_PHONE, KEY_IS_BLOCK_CALL, KEY_IS_BLOCK_SMS, KEY_IS_WHITE_LIST};
		String selection = KEY_PHONE + " = '" + phone.trim() + "'";
		String[] selectionAgrs = null;
		String groupBy = null;
		String having = null;
		String orderBy = null;
		return this.database.query(TABLE_BLACKLIST, columns, selection, selectionAgrs, groupBy, having, orderBy);
	}
	
	public Cursor selectWhiteListByPhone(String phone){
        String[] columns = {KEY_ID, KEY_NAME, KEY_PHONE, KEY_IS_BLOCK_CALL, KEY_IS_BLOCK_SMS, KEY_IS_WHITE_LIST};
        String selection = KEY_PHONE + " = '" + phone.trim() + "'";
        String[] selectionAgrs = null;
        String groupBy = null;
        String having = null;
        String orderBy = null;
        return this.database.query(TABLE_BLACKLIST, columns, selection, selectionAgrs, groupBy, having, orderBy);
    }
	
	private ContentValues initParameters(
			String fullname, 
			String phone, 
			String group, 
			String type,
			int isBlockCall,
			int isBlockSms,
			int isWhiteList){
		ContentValues parameters = new ContentValues();	
		parameters.put(KEY_NAME, fullname);
		parameters.put(KEY_PHONE, phone.trim());
		parameters.put(KEY_IS_BLOCK_CALL, isBlockCall);
		parameters.put(KEY_IS_BLOCK_SMS, isBlockSms);
		parameters.put(KEY_IS_WHITE_LIST, isWhiteList);
		
		return parameters;	
	}

	public long addBlackList(
			String fullname, 
			String phone, 
			String group, 
			String type,
			int isBlockCall,
            int isBlockSms,
            int isWhiteList){
		
		ContentValues parameters = initParameters(fullname, phone, group, type, isBlockCall, isBlockSms, isWhiteList);
		return this.database.insert(TABLE_BLACKLIST, null, parameters);
	}

	public boolean updateBlackList(
			int id,
			String name, 
			String phone, 
			String group, 
			String type,
            int isBlockCall,
            int isBlockSms,
            int isWhiteList){
		ContentValues parameters = initParameters(name, phone, group, type, isBlockCall, isBlockSms, isWhiteList);
		//update function return a number of row affected
		return this.database.update(TABLE_BLACKLIST, parameters, KEY_ID + " = " + id, null) > 0;
	}
	
	public boolean updateAdvanced(ContentValues parameters, String whereClause, String[] whereArgs){
		
		return this.database.update(TABLE_BLACKLIST, parameters, whereClause, whereArgs) > 0;
	}

	public boolean deleteBlackList(int _id){
		return this.database.delete(TABLE_BLACKLIST, KEY_ID + " = " + _id, null) > 0;
	}
	
	public boolean deleteAll(){
	    return this.database.delete(TABLE_BLACKLIST, null , null) > 0;
	}
	
	public boolean isPhoneExist(String phone){
		boolean isExist = false;
		Cursor cr = selectBlackListByPhone(phone);
		if(cr.getCount() > 0){
			isExist = true;
		}
		cr.close();
		return isExist;
	}
	
}